﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.Product.Bll;

namespace CIT.WMS.Product
{
    public partial class FrmInStockDetails : Form
    {
        public string InStockBillNo;

        public string OutStockBillNo;

        public string OutStockBillType;

        public DataTable BoxNoList;

        public List<BoxInfo> OutBoxList;

        public FrmInStockDetails()
        {
            InitializeComponent();
        }

        private void FrmInStockDetails_Load(object sender, EventArgs e)
        {
            InitFormData();

            DataTable dt = dtIvt.DataSource as DataTable;
            string boxNo = "";
            string boxStatus = "";
            int qty = 0;
            DataRow[] drList = this.BoxNoList.Select("BoxNo <> ''");
            foreach (DataRow dr in drList)
            {
                boxNo = dr["BoxNo"].ToString();
                boxStatus = "";

                if (string.IsNullOrEmpty(boxStatus))
                {
                    var realQty_Box = 0;
                    if (!string.IsNullOrEmpty(dr["RealQty"].ToString()))
                    {
                        realQty_Box = Convert.ToInt32(dr["RealQty"]);
                    }
                    if (dr["Status"].ToString().Equals("1") || realQty_Box == 0)
                    {
                        boxStatus = "已使用";
                    }
                }

                if (string.IsNullOrEmpty(boxStatus))
                {
                    var isExit = Bll_ProductOut.IsExitBill(this.OutStockBillNo, boxNo);
                    if (isExit)
                    {
                        boxStatus = "已出库";
                    }
                }

                if (string.IsNullOrEmpty(boxStatus) && this.OutStockBillType.Contains("返工出库单") == false)
                {
                    DataTable dateHoldBox = Bll_ProductOut.GetHoldBox(boxNo);
                    if (dateHoldBox != null && dateHoldBox.Rows.Count > 0)
                    {
                        boxStatus = "已冻结";
                    }
                }

                if (string.IsNullOrEmpty(boxStatus))
                {
                    qty += Int32.Parse(dr["RealQty"].ToString());
                }

                dt.Rows.Add(
                    boxNo,
                    dr["RealQty"].ToString(),
                    boxStatus
                    );
            }

            lab_Qty.Text = qty.ToString();
            if (qty == 0)
            {
                MessageBox.Show("该入库单号:" + this.InStockBillNo + "没有可以出库的箱！");
            }
        }

        private void btn_confirm_Click(object sender, EventArgs e)
        {
            DataTable dt = dtIvt.DataSource as DataTable;
            OutBoxList = new List<BoxInfo>();
            int qty = 0;
            foreach (DataRow dr in dt.Rows)
            {
                // 状态为空，并且箱号和数量不为空时可以出库
                if (string.IsNullOrEmpty(dr["Status"].ToString()) &&
                    !string.IsNullOrEmpty(dr["BoxNo"].ToString()) &&
                    !string.IsNullOrEmpty(dr["Qty"].ToString()))
                {
                    try
                    {
                       qty = Convert.ToInt32(dr["Qty"].ToString());
                    }
                    catch {
                        continue;
                    }
                    BoxInfo dto = new BoxInfo();
                    dto.BoxNo = dr["BoxNo"].ToString();
                    dto.Qty = qty.ToString();
                    OutBoxList.Add(dto);
                }
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void InitFormData()
        {
            OutBoxList = null;

            //编辑datatable
            DataTable dt = new DataTable();
            dt.Columns.Add("BoxNo");
            dt.Columns.Add("Qty");
            dt.Columns.Add("Status");
            dt.TableName = "Result";
            this.dtIvt.DataSource = dt;

            DataGridTableStyle dts = new DataGridTableStyle();
            dts.MappingName = "Result";

            DataGridTextBoxColumn column = new DataGridTextBoxColumn();
            column.Width = 135;
            column.HeaderText = "箱号";
            column.MappingName = "BoxNo";
            dts.GridColumnStyles.Add(column);

            column = new DataGridTextBoxColumn();
            column.Width = 56;
            column.HeaderText = "数量";
            column.MappingName = "Qty";
            column.Format = "";
            dts.GridColumnStyles.Add(column);

            column = new DataGridTextBoxColumn();
            column.HeaderText = "状态";
            column.MappingName = "Status";
            column.Width = 110;
            dts.GridColumnStyles.Add(column);

            this.dtIvt.TableStyles.Clear();
            this.dtIvt.TableStyles.Add(dts);
        }

        private void dtIvt_CurrentCellChanged(object sender, EventArgs e)
        {
            DataTable dt = dtIvt.DataSource as DataTable;
            lab_Qty.Text = dt.Select("BoxNo <> '' and Qty <> '' and Status = ''")
                .Select(x => {
                    try
                    {
                        return Convert.ToInt32(x["Qty"].ToString());
                    }
                    catch
                    {
                        return 0;
                    }
                })
                .Sum()
                .ToString();
        }
    }

    public class BoxInfo
    {
        public string BoxNo;

        public string Qty;

        public string PartNumber;
    }
}